#define RANGE 30
#define PINKRANGE 40

void clear_pink(mln::image2d<mln::value::rgb8>& input)
{
  using namespace mln;

  mln_piter_(image2d<value::rgb8>) p(input.domain());
  for_all(p)
  {
    unsigned int highred;
    unsigned int lowred;
    unsigned int lowblue;
    value::rgb8 old = input(p);

    if (input(p).red() < (unsigned int)(255 - RANGE))
      highred = input(p).red() + RANGE;
    else
      highred = 255;

    if (input(p).red() > RANGE)
      lowred = (unsigned int)(input(p).red() - RANGE);
    else
      lowred = 0;

    if (((input(p).green() >= lowred) &&
	 (input(p).blue() >= lowred)) &&
	((input(p).green() <= highred) &&
	 (input(p).blue() <= highred)))
      {
	input(p).red() = 0;
	input(p).green() = 255;
	input(p).blue() = 0;
      }

    if (input(p).red() > PINKRANGE)
      lowred = (unsigned int)(input(p).red() - PINKRANGE);
    else
      lowred = 0;

    if (input(p).blue() > PINKRANGE)
      lowblue = (unsigned int)(input(p).blue() - PINKRANGE);
    else
      lowblue = 0;

    if ((input(p).green() <= lowred) &&
	(input(p).blue() <= lowred))
      {
	input(p).red() = 255;
	input(p).green() = 255;
	input(p).blue() = 255;
      }
    else
      {
	if ((input(p).green() <= lowblue) &&
	    (input(p).red() <= lowblue))
	  {
	    input(p).red() = 0;
	    input(p).green() = 0;
	    input(p).blue() = 0;
	  }
	else
	  if ((input(p).red() == 0) && (input(p).green() == 255) && (input(p).blue() == 0)
	      && ((old.red() <= 126) || (old.blue() <= 126) || (old.green() <= 126)))
	    {
	      input(p).red() = 0;
	      input(p).green() = 0;
	      input(p).blue() = 0;
	    }
	  else
	    input(p) = old;
      }
  }
}
